import random
import time
from abc import ABC, abstractmethod
from typing import List, Dict, Any
import json


class AITrainingPartner(ABC):
    """AI陪练基类"""

    def __init__(self, name, difficulty="medium"):
        self.name = name
        self.difficulty = difficulty
        self.session_history = []
        self.user_level = "beginner"

    @abstractmethod
    def generate_exercise(self, topic):
        """生成练习题目"""
        pass

    @abstractmethod
    def evaluate_response(self, user_response, correct_answer):
        """评估用户回答"""
        pass

    @abstractmethod
    def provide_feedback(self, user_response, evaluation):
        """提供反馈"""
        pass

    def adjust_difficulty(self, performance):
        """根据表现调整难度"""
        if performance > 0.8:
            self.difficulty = self._increase_difficulty(self.difficulty)
        elif performance < 0.4:
            self.difficulty = self._decrease_difficulty(self.difficulty)

    def _increase_difficulty(self, current):
        levels = ["easy", "medium", "hard", "expert"]
        current_index = levels.index(current)
        return levels[min(current_index + 1, len(levels) - 1)]

    def _decrease_difficulty(self, current):
        levels = ["easy", "medium", "hard", "expert"]
        current_index = levels.index(current)
        return levels[max(current_index - 1, 0)]

    def record_session(self, topic, user_response, evaluation):
        """记录训练会话"""
        self.session_history.append({
            "timestamp": time.time(),
            "topic": topic,
            "user_response": user_response,
            "evaluation": evaluation,
            "difficulty": self.difficulty
        })